//
//  ViewController.m
//  仿微信扫街景
//
//  Created by 李志伟 on 15/8/24.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "StreetscapeViewController.h"

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (nonatomic,strong) AVCaptureDeviceInput *input;

@property (nonatomic,strong) AVCaptureMetadataOutput *output;

@property (nonatomic,strong) AVCaptureSession *session;

@property (nonatomic,strong) AVCaptureVideoPreviewLayer *prelayer;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航条颜色
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    
    //导航标题颜色
    NSDictionary *atrribute = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.navigationController.navigationBar.titleTextAttributes = atrribute;
    
    //导航按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    
  
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    self.input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    
    if (!_input) {
        //        NSLog(@"没有输入设备%@",error);
        return;
    }
    
    self.output = [[AVCaptureMetadataOutput alloc] init];
    
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    self.session = [[AVCaptureSession alloc] init];
    [self.session setSessionPreset:AVCaptureSessionPresetHigh];
    
    self.prelayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];

    if ([self.session canAddInput:self.input]) {
        [self.session addInput:self.input];
    }
    
    if ([self.session canAddOutput:self.output]) {
        [self.session addOutput:self.output];
    }
    
    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
    
    self.prelayer.frame = self.view.frame;
    
    [self.view.layer insertSublayer:self.prelayer atIndex:0];
    
    
    [self.session startRunning];
    
    
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self performSelector:@selector(getLocation:) withObject:nil afterDelay:2.0];
}


-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    
    
    NSLog(@"-----");
}



- (IBAction)getLocation:(id)sender {
    
//    sleep(2.0);
    
    StreetscapeViewController *streetVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"StreetscapeViewController"];
    
    [self.navigationController pushViewController:streetVC animated:YES];
    
}





@end
